package com.douwong.student.task;

import android.os.AsyncTask;
import android.util.Log;

import com.douwong.student.application.BaseApplication;
import com.douwong.student.contant.NetConfig;
import com.douwong.student.interfaces.WorkResultNotify;
import com.douwong.student.model.ClassExamRecordList;
import com.douwong.student.model.RequestAction;
import com.douwong.student.utils.JsonUtils;
import com.douwong.student.utils.NetUtils;
import com.douwong.student.utils.UIUtils;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * 课堂考试列表查询
 * @author yichao
 *
 */
public class ClassExamRecordListTask extends AsyncTask<String, Integer, Integer>{

	private WorkResultNotify<ClassExamRecordList.DataBean.ItemsBean> workResult = null;
	public static int NO_DATA = 0;// 没有新的数据
	public static int REQUESTOK = 1;// 加载更多
	public static int NO_NETWOK = -1;// 没有网络
	public static int ERROR;
	private RequestAction action;
	private BaseApplication app;
	private String key;
	private boolean isSearch=false;
    private List<ClassExamRecordList.DataBean.ItemsBean> mItems;

    public ClassExamRecordListTask(RequestAction action, BaseApplication app, boolean isSearch) {
		this.app = app;
		this.action = action;
		this.isSearch=isSearch;
	}

	public void setWorkStautsListener(WorkResultNotify<ClassExamRecordList.DataBean.ItemsBean> workResult) {
		this.workResult = workResult;
	}
	@Override
	protected Integer doInBackground(String... params) {
		int resultCode ;
		key = BaseApplication.sUserBean.getUserbean().getEuNumber() + "_classExam_"
				+ params[0];
		if (RequestAction.INIT == action) {
			if (app.readObject(key) != null) {
                mItems = (List<ClassExamRecordList.DataBean.ItemsBean>) app.readObject(key);
				return REQUESTOK;
			} else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
				resultCode = accessNetworkRequestData(params);
			} else {
				resultCode = NO_NETWOK;
			}
		} else if (NetUtils.NetwrokIsUseful(UIUtils.getContext())) {
			resultCode = accessNetworkRequestData(params);
		} else {
			resultCode = NO_NETWOK;
		}
		return resultCode;
	}
	private int accessNetworkRequestData(String... params) {
		String response = null;
		int resultCode ;
		List<BasicNameValuePair> arrayParams = new ArrayList<BasicNameValuePair>();
		arrayParams.add(new BasicNameValuePair("student_id", BaseApplication.sUserBean.getUserbean().getId()+""));
		arrayParams.add(new BasicNameValuePair("pagenum", "30"));
	//	arrayParams.add(new BasicNameValuePair("page", params[0]));
		if(params[1]!=null){
			arrayParams.add(new BasicNameValuePair("create_date", params[1]));
		}else {
			arrayParams.add(new BasicNameValuePair("create_date", ""));
		}
		if(params[2]!=null){
			arrayParams.add(new BasicNameValuePair("subjectid", params[2]));
		}else {
			arrayParams.add(new BasicNameValuePair("subjectid", "0"));
		}
		try {
			response = NetUtils
					.httpPost(NetConfig.CLASS_EXAM_RECORD_LIST+params[0], arrayParams);
			Log.e("response", response + "");

			if (response!=null){
				ClassExamRecordList classExamRecordList = JsonUtils.parseString(response, ClassExamRecordList.class);
				if (classExamRecordList!=null&&classExamRecordList.isFlag()) {
					//	Log.e("sc", classExamRecordList.getData().getRows() + "");
					mItems = classExamRecordList.getData().getItems();
					if (mItems ==null || mItems.size()==0){
						return NO_DATA;
					}
					app.saveObject(mItems,key);
					return  REQUESTOK;

				}else {
					return  NO_DATA;
				}
			}else {
				return NO_DATA;
			}

		} catch (Exception e) {
			e.printStackTrace();
			resultCode=NO_DATA;
		}
		return resultCode;
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
        if (workResult!=null){
		workResult.notify(result, mItems);
        }
	}

}
